package com.qmds.common.baidu.utils;

import com.baidu.aip.contentcensor.AipContentCensor;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * --- 天道酬勤 ---
 *
 * @author zhanghanxuan
 * @date 2024/8/26
 * @desc
 */
@Component
public class BaiduUtil {
    @Value("${baidu.APP_ID}")
    static final String APP_ID = "110900074";

    @Value("${baidu.API_KEY}")
    static final String API_KEY = "sJkmGiKxcSpfl68iIO6Xt53E";

    @Value("${baidu.SECRET_KEY}")
    static final String SECRET_KEY = "fu6fAikzoudVmzNVva0tlTz4shO5kPrH";



    @Autowired
    private AipContentCensor baiduClient;

    /**
     * 文本审核
     *
     * @param text 要审核的文本
     * @return 审核结果
     */
    public  int textCheck(String text) {
        JSONObject response = baiduClient.textCensorUserDefined(text);

        System.out.println(response.toString());
        // 是否合规的状态，1.合规，2.不合规，3.疑似，4.审核失败
        int conclusionType = response.getInt("conclusionType");
        System.out.println(conclusionType);
        // 获取不合规的数据
        if (conclusionType != 1) {
            JSONArray data = response.getJSONArray("data");
            for (int i = 0; i < data.length(); i++) {
                JSONObject jsonObject = data.getJSONObject(i);
                System.out.println(jsonObject.getString("msg"));
            }
        }
        return conclusionType;
    }


}
